<?php

/** Represents a venue.
 *
 * @package	php-lastfm-api
 * @author  Felix Bruns <felixbruns@web.de>
 * @version	1.0
 */
class Lastfm_Venue {

    /** The venues id.
     *
     * @var string
     * @access private
     */
    private $id;
    /** The venues name.
     *
     * @var string
     * @access private
     */
    private $name;
    /** The venues location.
     *
     * @var Location
     * @access private
     */
    private $location;
    /** The venues URL.
     *
     * @var string
     * @access private
     */
    private $url;
    /** The venues Website.
     *
     * @var string
     * @access private
     */
    private $website;
    /** An array of images of this venue.
     *
     * @var array
     * @access	private
     */
    private $images;

    /** Create a Venue object.
     *
     * @param string	$name		A venue name.
     * @param Location	$location	A venue location.
     * @param string	$url		A venue URL.
     *
     * @access public
     */
    public function __construct($id, $name, Lastfm_Location $location, $url, $website, $images) {
        $this->id = $id;
        $this->name = $name;
        $this->location = $location;
        $this->url = $url;
        $this->website = $website;
        $this->images = $images;
    }

    /** Returns the venues id.
     *
     * @return	string	A venue id.
     * @access public
     */
    public function getId() {
        return $this->id;
    }

    /** Returns the venues name.
     *
     * @return	string	A venue name.
     * @access public
     */
    public function getName() {
        return $this->name;
    }

    /** Returns the venues location.
     *
     * @return	Location	A venue location.
     * @access public
     */
    public function getLocation() {
        return $this->location;
    }

    /** Returns the venues URL.
     *
     * @return	string	A venue URL.
     * @access public
     */
    public function getUrl() {
        return $this->url;
    }

    /** Returns the venues Website.
     *
     * @return	string	A venue Website.
     * @access public
     */
    public function getWebsite() {
        return $this->website;
    }

    /** Returns an image URL of the specified size of this venue.
     *
     * @param	integer	$size	Image size constant. (Optional)
     * @return	string			An image URL.
     * @access	public
     */
    public function getImage($size = null) {
        if ($size != null) {
            if ($this->images[$size] == "")
                return "/img/noimage.gif";
            else
                return $this->images[$size];
        }

        for ($size = Lastfm_Media::IMAGE_ORIGINAL; $size > Lastfm_Media::IMAGE_UNKNOWN; $size--) {
            if (array_key_exists($size, $this->images)) {
                if ($this->images[$size] == "")
                    return "/img/noimage.gif";
                else
                    return $this->images[$size];
            }
        }

        return null;
    }

    /** Get a list of upcoming events at this venue.
     *
     * @param	string	$venue	The venue id to fetch the events for. (Required)
     * @return	array			An array of Event objects.
     *
     * @static
     * @access	public
     * @throws	Error
     */
    public static function getEvents($venue) {
        $xml = Lastfm_Caller_Factory::getDefaultCaller()->call('venue.getEvents', array(
                    'venue' => $venue
                ));

        $events = array();

        foreach ($xml->children() as $event) {
            $events[] = Lastfm_Event::fromSimpleXMLElement($event);
        }

        return array('totalPages' => (int) $xml->attributes()->totalPages, 'events' => $events);
    }

    public static function isValid($venue) {
        return Lastfm_Venue::getInfo($venue);
    }

    public static function getInfo($venue) {
        $events = Lastfm_Venue::getPastEvents($venue, 1, 1);
        return $events->current()->getVenue();
    }

    /** Get a paginated list of all the events held at this venue in the past.
     *
     * @param	string	$venue	The id for the venue you would like to fetch event listings for. (Required)
     * @param	integer	$limit	The maximum number of results to return. (Optional)
     * @param	integer	$page	The page of results to return. (Optional)
     * @return	PaginatedResult	A PaginatedResult object.
     * @see		PaginatedResult
     *
     * @static
     * @access	public
     * @throws	Error
     */
    public static function getPastEvents($venue, $limit = null, $page = null) {
        $xml = Lastfm_Caller_Factory::getDefaultCaller()->call('venue.getPastEvents', array(
                    'venue' => $venue,
                    'limit' => $limit,
                    'page' => $page
                ));

        $events = array();

        foreach ($xml->children() as $event) {
            $events[] = Lastfm_Event::fromSimpleXMLElement($event);
        }

        $perPage = Lastfm_Util::toInteger($xml['perPage']);

        return new Lastfm_PaginatedResult(
                Lastfm_Util::toInteger($xml['total']),
                (Lastfm_Util::toInteger($xml['page']) - 1) * $perPage,
                $perPage,
                $events
        );
    }

    /** Search for a venue by venue name .
     *
     * @param	string	$venue		The venue name you would like to search for. (Required)
     * @param	integer	$limit		The number of results to fetch per page. Defaults to 50. (Optional)
     * @param	integer	$page		The results page you would like to fetch. (Optional)
     * @param	string	$country	Filter your results by country. Expressed as an ISO 3166-2 code. (Optional)
     * @return	PaginatedResult		A PaginatedResult object.
     * @see		PaginatedResult
     *
     * @static
     * @access	public
     * @throws	Error
     */
    public static function search($venue, $limit = null, $page = null, $country = null) {
        $xml = Lastfm_Caller_Factory::getDefaultCaller()->call('venue.search', array(
                    'venue' => $venue,
                    'limit' => $limit,
                    'page' => $page,
                    'country' => $country
                ));

        $venues = array();

        foreach ($xml->venuematches->children() as $venue) {
            $venues[] = Lastfm_Venue::fromSimpleXMLElement($venue);
        }

        $opensearch = $xml->children('http://a9.com/-/spec/opensearch/1.1/');

        return new Lastfm_PaginatedResult(
                Lastfm_Util::toInteger($opensearch->totalResults),
                Lastfm_Util::toInteger($opensearch->startIndex),
                Lastfm_Util::toInteger($opensearch->itemsPerPage),
                $venues
        );
    }

    /** Create a Venue object from a SimpleXMLElement.
     *
     * @param	SimpleXMLElement	$xml	A SimpleXMLElement.
     * @return	Venue						A Venue object.
     *
     * @static
     * @access	public
     * @internal
     */
    public static function fromSimpleXMLElement(SimpleXMLElement $xml) {
        if ($xml->image) {
            if (count($xml->image) > 1) {
                foreach ($xml->image as $image) {
                    $images[Lastfm_Util::toImageType($image['size'])] = Lastfm_Util::toString($image);
                }
            } else {
                $images[Lastfm_Media::IMAGE_LARGE] = Lastfm_Util::toString($image);
            }
        }

        return new Lastfm_Venue(
                Lastfm_Util::toString($xml->id),
                Lastfm_Util::toString($xml->name),
                Lastfm_Location::fromSimpleXMLElement($xml->location),
                Lastfm_Util::toString($xml->url),
                Lastfm_Util::toString($xml->website),
                $images
        );
    }

}

